package com.asset.salesforce.dao;

import java.util.ArrayList;
import java.util.List;

import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.StockCategory;
import com.asset.salesforce.domain.StockProduct;
import com.asset.salesforce.domain.StockSku;
import com.asset.salesforce.domain.StockSubCategory;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class StockProductDaoImpl extends BasicDaoImpl<StockProduct>{

	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	public StockProduct getProductsById(Integer id){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from StockProduct where  id = :id");
		query.setParameter("id", id);
		List<StockProduct>  events = new ArrayList<StockProduct>();
		events= query.list();
		return events.get(0);
	}
	@SuppressWarnings("unchecked")
	public  StockProduct getByProName(String name) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from StockProduct where name=  :name");
		 query.setParameter("name", name);
		 List<StockProduct>list  = new ArrayList<StockProduct>();
		 list = query.list();
		 if(list.size() >0){
			 return list.get(0);
		 }else{
			 return null;
		 }
	
	}

	@SuppressWarnings("unchecked")
	public StockProduct getByProCode(String code) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from StockProduct where code=  :code");
		 query.setParameter("code", code);
		 List<StockProduct>list  = new ArrayList<StockProduct>();
		 list = query.list();
		 if(list.size() >0){
			 return list.get(0);
		 }else{
			 return null;
		 }
	
	}
	@SuppressWarnings("unchecked")
	public List<StockProduct> getAllProductsBySubCategory(Integer stockSubCategoryId,Integer employeeId) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from StockProduct where stockSubCategory.id = :stockSubCategoryId And owner.id=:employeeId");
		 query.setParameter("stockSubCategoryId", stockSubCategoryId);
		 query.setParameter("employeeId", employeeId);
		 List<StockProduct>list  = new ArrayList<StockProduct>();
		 list = query.list();
		 if(list.size() >0){
			 return list;
		 }else{
			 return new ArrayList<StockProduct>();
		 }
	
	}
	
	public List<StockProduct> getAllStockProductsByCompanyId(Employee employee){
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from StockProduct stockProduct where stockProduct.owner.company.id = :companyId");
		 query.setParameter("companyId", employee.getCompany().getId());
		 List<StockProduct>list  = new ArrayList<StockProduct>();
		 list = query.list();
			 return list;
	}
}
